package com.a1anwang.okble.demo;


import com.a1anwang.okble.client.core.OKBLEDevice;
import com.a1anwang.okble.client.core.OKBLEDeviceImp;
import com.a1anwang.okble.client.core.OKBLEDeviceListener;
import com.a1anwang.okble.client.core.OKBLEOperation;
import com.a1anwang.okble.client.scan.BLEScanResult;
import com.a1anwang.okble.client.scan.DeviceScanCallBack;
import com.a1anwang.okble.client.scan.OKBLEScanManager;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;

import ohos.app.Context;
import org.junit.Test;

import static org.junit.Assert.*;

public class MainAbilityTest {
    private OKBLEScanManager scanManager;

    private BLEScanResult device;
    private OKBLEDevice okbleDevice;

    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.a1anwang.okble.demo", actualBundleName);
    }

    @Test
    public void testScan(){
        scanManager = new OKBLEScanManager(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext());
        scanManager.setScanCallBack(scanCallBack);
        scanManager.startScan();
    }

    @Test
    public void testConnect(){
        if(okbleDevice == null){
            return;
        }
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        okbleDevice = new OKBLEDeviceImp(context);
        okbleDevice.setBluetoothDevice(device.getBluetoothDevice());
        okbleDevice.addDeviceListener(new OKBLEDeviceListener() {
            @Override
            public void onConnected(String deviceTag) {
                assertNotNull(deviceTag);
            }

            @Override
            public void onDisconnected(String deviceTag) {

            }

            @Override
            public void onReadBattery(String deviceTag, int battery) {

            }

            @Override
            public void onReceivedValue(String deviceTag, String uuid, byte[] value) {

            }

            @Override
            public void onWriteValue(String deviceTag, String uuid, byte[] value, boolean success) {

            }

            @Override
            public void onReadValue(String deviceTag, String uuid, byte[] value, boolean success) {

            }

            @Override
            public void onNotifyOrIndicateComplete(String deviceTag, String uuid, boolean enable, boolean success) {

            }
        });
        okbleDevice.connect(true);
    }

    @Test
    public void testDisconnect(){
        if(okbleDevice != null){
            okbleDevice.disConnect(false);
        }
    }

    @Test
    public void testRemoveDevice(){
        if(okbleDevice != null){
            okbleDevice.remove();
        }
    }

    @Test
    public void testWriteData(){
        if(okbleDevice == null){
            return;
        }
        okbleDevice.addWriteOperation(device.getBluetoothDevice().getDeviceUuids()[0].toString(), "Write Data", new OKBLEOperation.WriteOperationListener() {
            @Override
            public void onWriteValue(final byte[] byteValue) {
                assertNotNull(byteValue);
            }

            @Override
            public void onFail(int code, final String errMsg) {
            }

            @Override
            public void onExecuteSuccess(OKBLEOperation.OperationType type) {
                assertNotNull(type);
            }
        });
    }

    @Test
    public void testRead(){
        if(okbleDevice == null){
            return;
        }
        okbleDevice.addReadOperation(device.getBluetoothDevice().getDeviceUuids()[0].toString(), new OKBLEOperation.ReadOperationListener() {
            @Override
            public void onReadValue(final byte[] value) {
                assertNotNull(value);
            }

            @Override
            public void onFail(int code, final String errMsg) {
            }

            @Override
            public void onExecuteSuccess(OKBLEOperation.OperationType type) {
                assertNotNull(type);
            }
        });
    }

    // 扫描回调监听
    private final DeviceScanCallBack scanCallBack = new DeviceScanCallBack() {
        @Override
        public void onBLEDeviceScan(BLEScanResult device, int rssi) {
            assertNotNull(device);
            scanManager.stopScan();
            MainAbilityTest.this.device = device;
        }

        @Override
        public void onFailed(int code) {

        }

        @Override
        public void onStartSuccess() {

        }
    };
}